/*
Retorna todos los registros
short: registro corto, solos los fields indicados en el campo fields
fields: formato [<field1>,<field2>,..]
*/
module.exports.getAll = function getAll(model, short, fields, sortField, sortOrd, callback) {
	try {
		var fields = JSON.parse(fields);
		if (sortField) {
			model.find({}, (short ? fields : {})).sort((sortField ? sortField : ''), (sortOrd ? sortOrd : 1)).run(function(err, docs) {
			  	if (err) {
				  	console.log('ERROR: baseDao mongo short find ' + model.modelName + '. Detalle: ' + err);
			  	  	callback(err);
			  	} else
			  		callback(false, docs);
		   	});
		} else {
			model.find({}, (short ? fields : {}), function(err, docs) {
			  	if (err) {
				  	console.log('ERROR: baseDao mongo find ' + model.modelName + '. Detalle: ' + err);
			  	  	callback(err);
			  	} else
			  		callback(false, docs);
		   	});
		}
	} catch (err) {
		console.log('ERROR: baseDao getAll ' + model.modelName + '. Detalle: ' + err);
		callback(err);
	}
}

//Retorna un conjunto de datos de acuerdo a los filtros utilizados 
module.exports.find = function get(model, filters, one, callback) {
	try {
		if (one) {
			model.findOne(filters, function(err, doc) {
				if (err) {
					console.log('ERROR: baseDao mongo find ' + model.modelName + '. Detalle: ' + err);
				  	callback(err);
				} else
					callback(false, doc);
		    });
		} else {
			model.find(filters, function(err, docs) {
				if (err) {
					console.log('ERROR: baseDao mongo find ' + model.modelName + '. Detalle: ' + err);
				  	callback(err);
				} else
					callback(false, docs);
		    });
		}
		
	} catch (err) {
		console.log('ERROR: baseDao get ' + model.modelName + '. Detalle: ' + err);
		callback(err);
	}
}


//Crea un nuevo dato
module.exports.set = function set(model, data, callback) {
	try {
		var odata = new model(data);
		odata.save(function(err) {
		  	if (err) {
				  console.log('ERROR: baseDao mongo save ' + model.modelName + '. Detalle: ' + err);
		  	  	callback(err);
		  	} else
		  		callback();
	   	});
	} catch (err) {
		console.log('ERROR: baseDao set ' + model.modelName + '. Detalle: ' + err);
		callback(err);
	}
}

//Actualiza un dato existente
module.exports.update = function update(model, searchFieldName, searchFieldData, data, callback) {
	try {
		var oid = JSON.parse('{"' + searchFieldName + '":"' + searchFieldData + '"}');
		model.update(oid, data, function(err) {
			if (err) {
				console.log('ERROR: baseDao update ' + model.modelName + '. Detalle: ' + err);
		  	  	callback(err);
			} else
				callback();
		});
	} catch (err) {
		console.log('ERROR: baseDao update ' + model.modelName + '. Detalle: ' + err);
		callback(err);
	}
}

//Actualiza un dato existente
module.exports.upsert = function upsert(model, searchFieldName, searchFieldData, data, callback) {
	try {
		var oid = JSON.parse('{"' + searchFieldName + '":"' + searchFieldData + '"}');
		model.update(oid, data, {upsert: true}, function(err) {
			if (err) {
				console.log('ERROR: baseDao upsert ' + model.modelName + '. Detalle: ' + err);
		  	  	callback(err);
			} else
				callback();
		});
	} catch (err) {
		console.log('ERROR: baseDao upsert ' + model.modelName + '. Detalle: ' + err);
		callback(err);
	}
}

//Elimina un dato existente  
module.exports.remove = function remove(model, searchFieldName, searchFieldData, callback) {
	try {
		var oid = JSON.parse('{"' + searchFieldName + '":"' + searchFieldData + '"}');
		model.findOne(oid, function(err, data) {
		  	if (err) {
			  	console.log('ERROR: baseDao mongo findOne ' + model.modelName + '. Detalle: ' + err);
		  	  	callback(err);
			}
			if (data) {
				data.remove(function(err) {
				  	if (err) {
					 	console.log('ERROR: baseDao mongo remove ' + model.modelName + '. Detalle: ' + err);
			  	  		callback(err);
				  	}
				  	callback();
				});
			} else {
				callback('not found');
			}
	   });
	} catch (err) {
		console.log('ERROR: baseDao remove ' + model.modelName + '. Detalle: ' + err);
		callback(err);
	}
}

